﻿using System.Collections.Generic;
using System.IO;

namespace Peter.DataAccess.Sqlite
{
   /// <summary>
   /// Writes data to a sqlite database.
   /// </summary>
   public class SqliteWriter : SqliteAdapter
   {
      /// <summary>
      /// Initializes a new sqlite writer. THIS WILL DELETE YOUR DATABASE!
      /// </summary>
      /// <param name="pathToDatabase">Path to database to use.</param>
      public SqliteWriter (string pathToDatabase) : base (pathToDatabase)
      {
         if (File.Exists (pathToDatabase))
            File.Delete (pathToDatabase);
         this.Connect ();
      }

      /// <summary>
      /// Writes the given object to a sqlite database.
      /// </summary>
      /// <param name="data"></param>
      public void Write (object data)
      {
         var dataType = data.GetType ();
         var properties = dataType.GetProperties ();

         this.CreateTable ("", this.GetTableColumns ());
         foreach (var property in properties)
         {
            if (property.CanRead && property.CanWrite)
            {
               
            }
         }
      }

      /// <summary>
      /// Gets the list of columns for a table.
      /// </summary>
      /// <returns></returns>
      private IList <SqliteColumn> GetTableColumns ()
      {
         var columns = new List <SqliteColumn>
                          {
                             new SqliteColumn {Name = "Property", DataType = SqliteColumnType.Text},
                             new SqliteColumn {Name = "Type", DataType = SqliteColumnType.Integer},
                             new SqliteColumn {Name = "Value", DataType = SqliteColumnType.Blob}
                          };
         return columns;
      }
   }
}
